#include "NameErr.h"

#include <stdarg.h>
#include <string.h>
#include <fan-llvm/core/Core.h>
#include "Str.h"
#include "Type.h"


Type* NameErr_classType = NULL;
VTable* NameErr_classVTable = NULL;


/**********************************************
 *
 * Methods and fields
 *
 **********************************************/


/* Void make(Str msg, Err? cause) */
void NameErr_make_0(NameErr* self)
{
  NameErr_make(self, Str_defVal(), NULL);
}


/* Void make(Str msg, Err? cause) */
void NameErr_make_1(NameErr* self, Str* msg)
{
  NameErr_make(self, msg, NULL);
}


/* Void make(Str msg, Err? cause) */
void NameErr_make(NameErr* self, Str* msg, Err* cause)
{
  Err_make((Err*)self, msg, cause);
}


/**********************************************
 *
 * Extra methods
 *
 **********************************************/


/* Create a new NameErr instance and initialize it with NameErr_make() */
NameErr* NameErr_new(Str* msg, Err* cause)
{
  NameErr* err = (NameErr*)Obj_allocate(sizeof(NameErr), NameErr_classVTable);
  NameErr_make(err, msg, cause);
  return err;
}


/* Create a new NameErr instance and initialize it with the message given by the specified format.
 * 'msgFormat' uses the same format as Str_newWithFormat() */
NameErr* NameErr_newWithFormattedMsg(const char* msgFormat, ...)
{
  va_list args;
  va_start(args, msgFormat);
  Str* msg = Str_newWithFormatValist(msgFormat, args);
  va_end(args);
  return NameErr_new(msg, NULL);
}


/**********************************************
 *
 * Type-system functions
 *
 **********************************************/


Type* NameErr_initType(void)
{
  if (NameErr_classType == NULL)
  {
    Type* baseType = Err_initType();
    VTable* vtable = NameErr_initVTable();
    Type* type = Type_new("NameErr", baseType, vtable, sizeof(NameErr), sizeof(NameErrVTable));
    Type_registerCtorImplementation(type, "make", NameErr_make_0, 0);
    Type_registerCtorImplementation(type, "make", NameErr_make_1, 1);
    Type_registerCtorImplementation(type, "make", NameErr_make, 2);
    NameErr_classType = type;
    NameErr_initStatic();
  }
  return NameErr_classType;
}


VTable* NameErr_initVTable(void)
{
  if (NameErr_classVTable == NULL)
  {
    VTable* vtable = memoryAlloc(sizeof(NameErrVTable));
    memcpy(vtable, Err_initVTable(), sizeof(ErrVTable));
    NameErr_classVTable = vtable;
  }
  return NameErr_classVTable;
}


void NameErr_initStatic(void)
{
}
